package bid.yanhj.cniao5shop.cniao5shop.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;



import java.util.ArrayList;
import java.util.List;

import bid.yanhj.cniao5shop.cniao5shop.R;
import bid.yanhj.cniao5shop.cniao5shop.bean.Tab;
import bid.yanhj.cniao5shop.cniao5shop.fragment.CartFragment;
import bid.yanhj.cniao5shop.cniao5shop.fragment.CategoryFragment;
import bid.yanhj.cniao5shop.cniao5shop.fragment.HomeFragment;
import bid.yanhj.cniao5shop.cniao5shop.fragment.MineFragment;
import bid.yanhj.cniao5shop.cniao5shop.widget.FragmentTabHost;

/**
 *  see https://www.jianshu.com/p/4d4a83945193
 *  https://developer.android.google.cn/reference/androidx/fragment/app/FragmentTabHost
 *
 *  广告轮播图
 *  https://github.com/daimajia/AndroidImageSlider
 */
public class MainActivity extends AppCompatActivity {

    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    private List<Tab> mTabs = new ArrayList<>();
    private Toolbar mToolBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        setContentView(R.layout.activity_test);


        mToolBar = findViewById(R.id.tool_bar);

        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "back", Toast.LENGTH_SHORT).show();
            }
        });

        mToolBar.inflateMenu(R.menu.meun_main);
//
        mToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int id = item.getItemId();
//                if(id == R.id.action_setting) {
//
//                }
                return false;
            }
        });

/**
 *  FragmentTabHost 部分实现
 * ==============================================
 *
 */
//        initTab();
//        mInflater = LayoutInflater.from(this);
//
//        mTabHost = (FragmentTabHost)findViewById(R.id.tabhost);
//
//        // TODO 空指针
//        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//
//        TabHost.TabSpec tabSpec = mTabHost.newTabSpec("home");
//
//        View view = mInflater.inflate(R.layout.tab_indicator, null);
//
//        ImageView img = findViewById(R.id.icon_tab);
//        TextView text = findViewById(R.id.text_indicator);
//
//        img.setBackgroundResource(R.mipmap.tab_home_select);
//        text.setText("主页");
//
//        tabSpec.setIndicator(view);
//        mTabHost.addTab(tabSpec, HomeFragment.class, null);

    }

    public void initTab() {
        Tab tab_home = new Tab(HomeFragment.class,R.string.home,R.mipmap.tab_home_select);
        Tab tab_hot = new Tab(HomeFragment.class,R.string.hot,R.mipmap.tab_find_select);
        Tab tab_category = new Tab(CategoryFragment.class,R.string.catagory,R.mipmap.tab_shop_select);
        Tab tab_cart = new Tab(CartFragment.class,R.string.cart,R.mipmap.tab_shop_select);
        Tab tab_mine = new Tab(MineFragment.class,R.string.mine,R.mipmap.tab_owner_select);

        mTabs.add(tab_home);
        mTabs.add(tab_hot);
        mTabs.add(tab_category);
        mTabs.add(tab_cart);
        mTabs.add(tab_mine);


        for (Tab tab : mTabs){

            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));

            tabSpec.setIndicator(buildIndicator(tab));

            mTabHost.addTab(tabSpec,tab.getFragment(),null);

        }

        mTabHost.setCurrentTab(0);
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
    }

    //构建Indicator
    private View buildIndicator(Tab tab){

        View view =mInflater.inflate(R.layout.tab_indicator,null);
        ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
        TextView text = (TextView) view.findViewById(R.id.text_indicator);

        img.setBackgroundResource(tab.getIcon());
        text.setText(tab.getTitle());

        return  view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mTabHost = null;
    }
}
